
import requests
import json
import dashscope
# from utils.log_wrapper import logger
import requests
# def chat(tokenizer,Q,history):
#     messages = [{'role': 'system', 'content': '你是一个专业的反诈骗民警，你可以准确的捕捉说话人要表达的意思。'}]
#     messages.append({"role": "user", "content": Q})
#     response = dashscope.Generation.call(
#         dashscope.Generation.Models.qwen-max,
#         messages=messages,
#         result_format='message',
#     )
#     print(f"Q(chat): {Q}")
#     print(f"A: {response['output']['choices'][0]['message']['content']}")
#     return response['output']['choices'][0]['message']['content'], history


def chat(tokenizer, user_message):
    # API端点
    url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'

    # 请求头
    headers = {
        'Authorization': f'Bearer sk-33e404ec0ad944fa97f7b51c0cc94660',
        'Content-Type': 'application/json'
    }

    # 请求数据
    data = {
        "model": "qwen-max",
        "input": {
            "messages": [      
                {
                    "role": "system",
                    "content": "You are a helpful assistant."
                },
                {
                    "role": "user",
                    "content": user_message
                }
            ]
        },
        "parameters": {
        }
    }

    # 发送POST请求
    response = requests.post(url, headers=headers, json=data)

    # 检查请求是否成功
    if response.status_code == 200:
        # 请求成功，处理响应数据
        return response.json()['output']['text']
    else:
        # 请求失败，输出错误信息
        return f"Error: {response.status_code}, {response.text}"

# 使用示例
# 注意：替换'<your-dashscope-api-key>'为您的实际API密钥

tokenizer = ""
response = chat(tokenizer, '你好，哪个公园距离我最近？')
print(response)


